I am trying to write a PowerShell script that will restart the SNMP Service on a server. The server in question is an HP server that has the various HP Insight agents installed, and some of these depend on the SNMP Service. One of these services also depends on one of the other HP Insight services (that in turn depends on SNMP Service).
I can easily write a script that stops and starts the specific services in order, but I don't want to do that. I like to create scripts that are as flexible as possible. In this case, I'd like to write a script that takes a service name as input
and can restart that service, including any dependents (and dependents of those dependents, and dependents of those dependents). They way to do this seems to be recursion, but I simply cannot get it to work correctly. Here is what I have so far
(just trying to get the services to stop in the correct order):
$MainServiceName = "SNMP" $ServiceInput = "" $CurrentService = "" $Service = "" function Custom-Stop-Service ($ServiceInput) { Write-Host "Name of `$ServiceInput: $($ServiceInput.Name)" Write-Host "Number of dependents: $($ServiceInput.DependentServices.Count)" If ($ServiceInput.DependentServices.Count -gt 0) { ForEach ($Service in $ServiceInput.DependentServices) { Write-Host "Dependent of $($ServiceInput.Name): $($Service.Name)" If ($Service.Status -eq "Running") { Write-Host "$($Service.Name) is running." $CurrentService = Get-Service -Name $Service.Name Custom-Stop-Service $CurrentService Write-Host "Stopping service $($CurrentService.Name)" Stop-Service -Name $CurrentService.Name -Force } Else { Write-Host "$($Service.Name) is stopped." } } } Else { Write-Host "Stopping service $($ServiceInput.Name)" Stop-Service -Name $ServiceInput.Name } } $MainService = Get-Service -Name $MainServiceName If ($MainService.Status -eq "Stopped") { Write-Host "Service $($MainService.Name) is already stopped." } Else { Custom-Stop-Service $MainService }
However, it doesn't work like I expect. Firstly, it never attempts to stop the SNMP service itself. Secondly while it does seem to stop all the dependents of SNMP correctly, it tries to stop CqMgHost twice. Here is the output:
PS C:\Scripts> .\restart_service.ps1 Name of $ServiceInput: SNMP Number of dependents: 4 Dependent of SNMP: CqMgHost CqMgHost is running. Name of $ServiceInput: CqMgHost Number of dependents: 0 Stopping service CqMgHost WARNING: Waiting for service 'HP Insight Foundation Agents (CqMgHost)' to finish stopping... WARNING: Waiting for service 'HP Insight Foundation Agents (CqMgHost)' to finish stopping... WARNING: Waiting for service 'HP Insight Foundation Agents (CqMgHost)' to finish stopping... WARNING: Waiting for service 'HP Insight Foundation Agents (CqMgHost)' to finish stopping... Stopping service CqMgHost Dependent of SNMP: CqMgStor CqMgStor is running. Name of $ServiceInput: CqMgStor Number of dependents: 1 Dependent of CqMgStor: CqMgHost CqMgHost is stopped. Stopping service CqMgStor Dependent of SNMP: CqMgServ CqMgServ is running. Name of $ServiceInput: CqMgServ Number of dependents: 1 Dependent of CqMgServ: CqMgHost CqMgHost is stopped. Stopping service CqMgServ WARNING: Waiting for service 'HP Insight Server Agents (CqMgServ)' to finish stopping... WARNING: Waiting for service 'HP Insight Server Agents (CqMgServ)' to finish stopping... Dependent of SNMP: CpqNicMgmt CpqNicMgmt is running. Name of $ServiceInput: CpqNicMgmt Number of dependents: 1 Dependent of CpqNicMgmt: CqMgHost CqMgHost is stopped. Stopping service CpqNicMgmt
Can someone suggest a different script or changes to my script so that it will do what I want? I know that "Stop-Service -Name SNMP -Force" will work but there are two problems with this: 1) it doesn't record the stop events for the dependents in the Event Viewer and 2) it doesn't work in reverse ("Start-Service -Name SNMP -Force" nor "Restart-Service -Name SNMP -Force" restart the main service and the other services that depend on SNMP that were stopped).
I'm a little surprised that Restart-Service doesn't behave like the restart function in the Services MMC does, where it easily and reliably handles the stops and starts of not only the service itself, but dependent services and also properly does event logging.